package com.tm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.entity.CategoryEntity;
import com.tm.service.CategoryService;
import com.tm.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author DELL
 * @PROJECT_NAME: tm-wm-syb
 * @DESCRIPTION:
 * @USER: DELL
 * @DATE: 2023/4/4 14:11
 */
@Api(value = "分类管理")
@RequestMapping("category")
@RestController
public class CategoryController {

    @Resource
     private CategoryService categoryService;
    @ApiOperation("查询分类")
    @GetMapping("page")
    public R page(@RequestParam(required = false,defaultValue = "1") Integer page,
                  @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        Page<CategoryEntity> categoryEntityPage = categoryService.pageList(page, pageSize);
        return R.success(categoryEntityPage);
    }

    @ApiOperation("新增分类")
    @PostMapping
    public R addcategory(@RequestBody CategoryEntity categoryEntity){
        R r = new R();
        categoryEntity.setCreateTime(new Date());
        categoryEntity.setCreateUser(1L);
        categoryEntity.setUpdateUser(1L);
        categoryEntity.setUpdateTime(new Date());
        categoryService.save(categoryEntity);

        return R.success();
    }
    @ApiOperation("删除分类")
    @DeleteMapping
    public R deletec(@RequestParam Long ids){
        categoryService.deletec(ids);
        return R.success();
    }
    @ApiOperation("修改分类")
    @PutMapping
    public R update(@RequestBody CategoryEntity categoryEntity){
         categoryService.updateById(categoryEntity);
        return R.success();
    }
    @GetMapping("list")
    public R catlist(){
        List<CategoryEntity> list = categoryService.list();
        return R.success(list);
    }
}
